#define _CRT_SECURE_NO_WARNINGS 1
#include"Tree.h"

//int main()
//{
//	int a[] = { 100, 48, 25, 36, 50, 16, 116 };
//	HP hp;
//	HeapInit(&hp);
//	for (int i = 0; i < sizeof(a) / sizeof(int); i++)
//	{
//		HeapPush(&hp, a[i]);
//	}
//	HeapPrint(&hp);
//	while (!HeapEmpty(&hp))
//	{
//		printf("%d ", HeapTop(&hp));
//		HeapPop(&hp);
//	}
//
//	HeapDestroy(&hp);
//
//	return 0;
//
//}
void PrintTopK(const char* filename, int k)
{
	FILE* fp = fopen(filename, "r");
	if (fp == NULL)
	{
		perror("fopen error");
		return;
	}
	int* minheap = (int*)malloc(sizeof(int) * k);
	if (minheap == NULL)
	{
		perror("malloc perror");
		return;
	}
	for (int i = 0; i < k; i++)
	{
		fscanf(fp, "%d", &minheap[i]);
	}
	for (int i = (k - 2) / 2; i >= 0; --i)
	{
		AdjustDown(minheap, k, i);
	}
	int x = 0;
	while (fscanf(fp, "%d", &x) != EOF)
	{
		if (x > minheap[0])
		{
			minheap[0] = x;
			AdjustDown(minheap, k, 0);
		}
	}
	for (int i = 0; i < k; i++)
	{
		printf("%d ", minheap[i]);
	}
	printf("\n");
	fclose(fp);
}
void TestTopk()
{
	int n = 10000;
	int* a = (int*)malloc(sizeof(int) * n);
	srand(time(0));
	const char* file = "data.txt";
	FILE* fp = fopen(file, "w");
	if (fp == NULL)
	{
		perror("fopen error");
		return;
	}
	for (int i = 0; i < n; ++i)
	{
		int x = rand() % 1000000;
		fprintf(fp, "%d\n", x);
	}
	fclose(fp);
	PrintTopK(file,10);
}
int main()
{
	TestTopk();
	return 0;
}